<?php

declare(strict_types=1);

namespace app\common\model;

use app\common\tool\excel\Export;
class Signup extends Base
{

    protected $name = 'signup';
    public function getSexTextAttr($value, $data)
    {
        return $data['sex']==1 ? '男' : '女';
    }
    public function getCateTextAttr($value, $data)
    {
        $cate = Cate::where('id', $data['cate_id'])->value('title');
        return $cate??"";
    }
    /**
     * 数据
     */
    public static function getTakeOverTableList($that, $param, $filed = '*', $count = 0)
    {
        try {
            if (isset($param['export']) && $param['export'] == 1) {
                $datalist = [];
                foreach ($that as $key => $v) {
                    $datainfo = [
                        $v->rname,
                        $v->tel,
                        $v->sexText,
                        $v->age,
                        $v->birth,
                        $v->id_card,
                        $v->content,
                        $v->cateText,
                        $v->create_time,
                    ];
                    $datalist[] = $datainfo;
                }
                $datatitle = [
                    '姓名',
                    '电话',
                    '性别',
                    '年龄',
                    '出生日期',
                    '身份证号',
                    '工作经历',
                    '课程等级',
                    '报名时间',
                ];
                if (count($that) > 0) {
                    return ['code' => 0, 'data' => Export::export($datatitle,  $datalist, "uploads/signup/报名列表.xlsx")];
                } else {
                    return ['code' => 0, 'data' => 0];
                }
            }
            return ['code' => 0, 'data' => $that, 'count' => $count];
        } catch (\Exception $e) {
            return [];
        }
    }
    /**
     * 报名
     */
    public static function add($param)
    {
        try {
            $data = [
                'rname' => $param['rname'],
                'tel' => $param['tel'],
                'sex' => $param['sex'],
                'age' => $param['age'],
                'birth' => $param['birth'],
                'id_card' => $param['id_card'],
                'content' => $param['content'],
                'cate_id' => $param['cate_id'],
            ];
            $info = parent::where('tel',$param['tel'])->find();
            if($info){
                return ['code'=>-1,'msg'=>'该手机号已报名'];
            }
            parent::create($data);
            return ['code'=>1,'msg'=>'报名成功'];
        }catch (\Exception $e) {
            return ['code'=>-1,'msg'=>$e->getMessage()];
        }   
    }
}
